import Vue from 'vue';
import { createPinia, PiniaVuePlugin } from 'pinia';
import App from './App.vue';
import router from './router/index.js';
import store from './store';
import { globalRegister } from '../global-state';
import { useGlobalState } from '@/store/globalState.js';
import filters from '@/filters';
import Element, { Message } from 'element-ui';
// 分页组件
import Pagination from '@/components/Pagination';
import directive from './directive'; // directive
import { getDicts } from '@/api/system/dict/data';
// 自定义表格工具组件
import RightToolbar from '@/components/RightToolbar';
// 字典标签组件
import DictTag from '@/components/DictTag';
// 字典数据组件
import DictData from '@/components/DictData';
import plugins from './plugins';
import { download } from '@/utils/request';
// import { getSellDictCollection } from '@/api/zcqjy/index.js';
// import '@/utils/preventClick'

//搜索数据
// getSellDictCollection().then(res => {
//   if (res.data) {
//     store.dispatch('change_searParams', res.data);
//   }
// });
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key]);
});

Vue.use(Element);
Vue.config.productionTip = false;
Vue.use(directive);
Vue.use(plugins);
Vue.use(PiniaVuePlugin);
const pinia = createPinia();
DictData.install();

import '@/assets/css/project.scss';
import '@/assets/css/ruoyi.scss';
import 'element-ui/lib/theme-chalk/index.css';

/* main.js */
// 全局方法挂载
import * as echarts from 'echarts';
Vue.prototype.$echarts = echarts;
import { parseTime, resetForm, addDateRange, selectDictLabel, selectDictLabels, handleTree } from '@/utils/ruoyi';
Vue.prototype.handleTree = handleTree;
Vue.prototype.resetForm = resetForm;
Vue.prototype.getDicts = getDicts;
Vue.prototype.parseTime = parseTime;
Vue.prototype.download = download;
Vue.component('Pagination', Pagination);
Vue.component('RightToolbar', RightToolbar);
Vue.component('DictTag', DictTag);
let install = null;
// window.proxy.addEventListener('resize',()=>{
//     console.log('用原生js监听窗口变化')
// })
function render(props = {}) {
  console.log('🚀 ~ file: main.js:67 ~ render ~ props:', props);
  const { container } = props;
  install = new Vue({
    router,
    store,
    pinia,
    render: h => h(App),
  }).$mount(container ? container.querySelector('#microo-app') : '#microo-app');
  useGlobalState().setGlobalState(props);
}

if (window.__POWERED_BY_QIANKUN__) {
  // eslint-disable-next-line
  __webpack_public_path__ = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__;
} else {
  // 这里是子应用独立运行的环境，实现子应用的登录逻辑
  // 独立运行时，也注册一个名为global的store module
  globalRegister(store);
  render();
}

export async function bootstrap() {}

export async function mount(props) {
  globalRegister(store, props);
  render(props);
  // 容器显示高度
  document.getElementById('microo-app').style.height = document.getElementById('subapp-viewport').clientHeight + 'px';
}
export async function unmount() {
  install.$destroy();
  install.$el.innerHTML = ''; // 子项目内存泄露问题
  install = null;
  // router = null;
}
// export async function onBeforeUnmount() {
//     install.unmount()
// }

// const that = this;
Vue.directive('filterInput', {
  bind(el) {
    let ele = el.querySelector('input');
    // let ele = el.tagName === 'INPUT' ? el : el.querySelector('input');
    ele.addEventListener(
      'input',
      () => {
        let temp = ele.value;
        var pattern = new RegExp('[%!#$^&=|{};,\\[\\]<>！#￥……&|{}【】‘；：?]');
        // var patt = /^[\s]*$/;
        if (temp.match(pattern)) {
          temp = temp.replace(pattern, '');
          if (document.getElementsByClassName('el-message').length === 0) {
            // alert('禁止输入敏感字符！')
            Message({
              type: 'warning',
              message: `禁止输入敏感字符！`,
            });
          }
        }
        // else if(temp.match(new RegExp("\\s"))){
        //   temp = temp.trim();
        // }
        ele.value = temp;
        return temp;
      },
      true,
    );
  },
});

Vue.directive('preventClick', {
  inserted(el) {
    el.addEventListener('click', () => {
      if (!el.disabled) {
        el.disabled = true;
        setTimeout(() => {
          el.disabled = false;
        }, 1500);
      }
    });
  },
});
